package com.dora.mega.data.transfer.controller;

import com.dora.mega.data.transfer.service.IActResultLogService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * excel控制器
 *
 * @author Clarence
 * @date 2024/04/04
 */
@RequestMapping("/excel")
@RestController
public class ActResultController {
    @Resource
    private IActResultLogService actResultLogService;

    /**
     * 导出excel：一次性导出百万数据，容易OOM
     *
     * @param response 响应
     * @throws IOException ioexception
     */
    @GetMapping("/export")
    public void exportExcel(HttpServletResponse response) throws IOException {
        actResultLogService.exportExcel(response);
    }

    /**
     * 导出excel：分页查询处理，只是解决了OOM，但速度不快
     *
     * @param response 响应
     * @throws IOException          ioexception
     * @throws InterruptedException 中断异常
     */
    @GetMapping("/export1")
    public void exportExcel1(HttpServletResponse response) throws IOException, InterruptedException {
        actResultLogService.exportExcel1(response);
    }

    /**
     * 导出excel：多线程读取数据，随后放入多个map处理,最终放入一个sheet中
     *
     * @param response 响应
     * @throws IOException ioexception
     */
    @GetMapping("/export2")
    public void exportExcel2(HttpServletResponse response) throws IOException, InterruptedException {
        actResultLogService.exportExcel2(response);
    }

    /**
     * 导出excel：多线程读取数据，随后放入多个map处理,最终放入多个sheet中
     *
     * @param response 响应
     * @throws IOException ioexception
     */
    @GetMapping("/export3")
    public void exportExcel3(HttpServletResponse response) throws IOException, InterruptedException {
        actResultLogService.exportExcel3(response);
    }


    /**
     * 导入excel，单个sheet导入实现
     *
     * @param file    多部分文件
     * @param request 请求
     */
    @PostMapping("/import")
    public void importExcel(MultipartFile file, HttpServletRequest request) throws IOException {
        actResultLogService.importExcel(file);
    }

    /**
     * 导入excel，多个sheet导入，实现
     *
     * @param file    多部分文件
     * @param request 请求
     */
    @PostMapping("/import1")
    public void importExcel1(MultipartFile file, HttpServletRequest request) throws IOException {
        actResultLogService.importExcel1(file);
    }

    /**
     * 导入excel，单个sheet导入，实现
     *
     * @param file    文件
     * @param request 请求
     * @throws IOException ioexception
     */
    @PostMapping("/import2")
    public void importExcel2(MultipartFile file, HttpServletRequest request) throws IOException {
        // todo 若是单个sheet，可以根据行数进行区分，进行导入
        //actResultLogService.importExcel2(file);
    }
}
